import { Component, Vue } from "vue-property-decorator";
import { Action, Getter } from "vuex-class";
import XEUtils from "xe-utils";
import { cloneDeep } from "lodash";
import userPark from "@/utils/data/park";
import store from "@/store";
import { set } from "lodash";
import { layoutMixin } from "@/layout/Mixin";
@Component({
  mixins: [layoutMixin]
})
export class parkMixin extends Vue {
  @Action("sharing/all") getSharing: any;
  // 菜单
  @Getter("coreMenu/aside") asideData: any;
  @Action("coreMenu/accessLoad") accessLoad: any;
  // 用户信息
  @Getter("coreAccount/info") userInfo: any;

  onItemlick({ row }: any) {
    const me = this as any;
    userPark.set(row);
    me.clearCacheStore();
    if (me.userInfo.user_type == 2) {
      me.accessLoad(true).then(() => {
        me.loadSharing();
      });
    } else {
      me.loadSharing();
    }
  }

  loadSharing() {
    const me = this as any;
    // 获取小区字典数据
    me.getSharing().then(() => {
      setTimeout(() => {
        // 找到当前路由对应菜单数据
        const data = XEUtils.findTree(me.asideData, (item: any) => {
          return item.path == "parkManagement";
        });
        if (data) {
          const router = cloneDeep(data.item);
          // 路由type设置为1保证菜单展示正确
          router.type = 1;
          me.open(router);
        } else {
          userPark.remove();
        }
      }, 100);
    });
  }
  @Action("corePage/closeAll") closeAll: any;
  clearCacheStore() {
    // 清除缓存的vuex数据
    const list = [
      // 部门
      "purviewDepartment.tree",
      // 角色
      "purviewRole.all",
      // 角色菜单
      "purviewRole.agentMenu",
      "purviewRole.agentParkMenu",
      "purviewRole.yardParkmenu",
      // 员工
      "servicesStaff.all",
      // 住户组织
      "parkOrg.tree",
      // 设备
      "equipmentCate.catelist",
      "equipmentDoorGroup.all",
      "equipmentDoor.passDoor",
      "equipmentDoor.operateDoor"
    ],
      state = store.state;
    list.forEach((item) => {
      set(state, item, []);
    });
    // 关闭全部选项卡
    // 这样就可以执行获取全局数据的方法来获取数据了
    this.closeAll();
  }
}
